package com.taoes.simpledocker.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.github.dockerjava.api.DockerClient;
import com.github.dockerjava.api.model.Info;
import com.github.dockerjava.api.model.Version;
import com.github.xiaoymin.knife4j.annotations.ApiSort;
import com.taoes.simpledocker.config.DockerClientFactory;
import com.taoes.simpledocker.model.Role;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 * descip: Docker信息
 *
 * @author lfs
 * @date 2021/12/4 11:35 下午
 */
@ApiSort(value = 2)
@Api(tags = "Docker信息")
@RestController
@RequestMapping("/api/info")
@RequiredArgsConstructor
public class InfoController {

    private final DockerClientFactory clientFactory;

    @ApiOperation("查看Docker信息")
    @SaCheckPermission(value = "docker:query",orRole = Role.ADMIN_ROLE_NAME)
    @GetMapping
    public Map<String, Object> info() {
        final DockerClient client = clientFactory.get();
        final Info info = client.infoCmd().exec();
        final Version version = client.versionCmd().exec();

        Map<String,Object> result = new HashMap<>();
        result.put("info", info);
        result.put("version", version);
        return result;
    }
}
